"""9.2 访问安全控制策略—sse协议认证"""
import asyncio

from openai import OpenAI

from connect_factory import ServerManager



class MCPClient:



    def __init__(self, manager: ServerManager):
        # self.async_exit_stack = AsyncExitStack()
        self.session = None
        self.deepseek = OpenAI(
            api_key="sk-15af4e21f828460683b16ce9e78b2346",
            base_url="https://api.deepseek.com"
        )
        self.manager = manager


    async def execute(self,question:str):

        function_name = "add_tool"
        arguments = {
            "x":10,
            "y":20
        }
        # 五、调用工具链
        tool_result = await self.manager.execute_call(function_name,arguments)
        print("==== 工具调用结果tool_result：",tool_result)
        #最终的结果
        result = tool_result
        print(" 工具执行结果：",result)





async def main():
    #初始化状态信息
    manager = ServerManager()
    await manager.initialize()
    client = MCPClient(manager)
    try:
        while(True):
            #10加20之和再减去3等于多少
            question = input("请输入问题：")
            if question == "exit":
                break
            await client.execute(question)
    except Exception as e:
        print(f"连接失败: {e}")
        return
    finally:
        await manager.aclose()

if __name__ == "__main__":
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(main())